package com.smart.translator.ui.device;

import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.smart.translator.R;
import java.util.ArrayList;
import java.util.List;

public class ClassicBluetoothAdapter extends RecyclerView.Adapter<ClassicBluetoothAdapter.ViewHolder> {
    
    private List<BluetoothDevice> devices = new ArrayList<>();
    private OnDeviceClickListener listener;
    
    public interface OnDeviceClickListener {
        void onDeviceClick(BluetoothDevice device);
    }
    
    public void setOnDeviceClickListener(OnDeviceClickListener listener) {
        this.listener = listener;
    }
    
    public void setDevices(List<BluetoothDevice> devices) {
        this.devices = devices;
        notifyDataSetChanged();
    }
    
    public void addDevice(BluetoothDevice device) {
        // 检查是否已存在
        for (BluetoothDevice existingDevice : devices) {
            if (existingDevice.getAddress().equals(device.getAddress())) {
                return;
            }
        }
        devices.add(device);
        notifyItemInserted(devices.size() - 1);
    }
    
    public void clearDevices() {
        devices.clear();
        notifyDataSetChanged();
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_classic_bluetooth_device, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BluetoothDevice device = devices.get(position);
        holder.bind(device);
    }
    
    @Override
    public int getItemCount() {
        return devices.size();
    }
    
    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvDeviceName;
        private TextView tvDeviceAddress;
        private TextView tvDeviceType;
        
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDeviceName = itemView.findViewById(R.id.tv_device_name);
            tvDeviceAddress = itemView.findViewById(R.id.tv_device_address);
            tvDeviceType = itemView.findViewById(R.id.tv_device_type);
            
            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && listener != null) {
                    listener.onDeviceClick(devices.get(position));
                }
            });
        }
        
        public void bind(BluetoothDevice device) {
            String deviceName = device.getName();
            if (deviceName == null || deviceName.isEmpty()) {
                deviceName = "未知设备";
            }
            tvDeviceName.setText(deviceName);
            tvDeviceAddress.setText(device.getAddress());
            
            // 显示设备类型
            String deviceType = getDeviceTypeString(device.getType());
            tvDeviceType.setText(deviceType);
        }
        
        private String getDeviceTypeString(int type) {
            switch (type) {
                case BluetoothDevice.DEVICE_TYPE_CLASSIC:
                    return "经典蓝牙";
                case BluetoothDevice.DEVICE_TYPE_LE:
                    return "低功耗蓝牙";
                case BluetoothDevice.DEVICE_TYPE_DUAL:
                    return "双模蓝牙";
                case BluetoothDevice.DEVICE_TYPE_UNKNOWN:
                default:
                    return "未知类型";
            }
        }
    }
} 